package com.iteima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iteima.reggie.common.CustomException;
import com.iteima.reggie.dto.SetmealDto;
import com.iteima.reggie.entity.DishFlavor;
import com.iteima.reggie.entity.Setmeal;
import com.iteima.reggie.entity.SetmealDish;
import com.iteima.reggie.mapper.SetmealMapper;
import com.iteima.reggie.service.SetmealDishService;
import com.iteima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
        @Autowired
        private SetmealDishService setmealDishService;
    /**
     * 新增套餐,同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        //保存套餐的基本信息,操作setmeal,执行insert操作
        this.save(setmealDto);

        //保存套餐和菜品的关联关系,操作setmeal_dish 执行insert操作
        //对setmeal_Dish表中的套餐setmealId进行赋值
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishList);
    }
    /**
     * 删除套餐与对应菜品
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //判断该套餐是可不可以删
        //启售中不可以删除
        //select  count(*) from setmeal in(1,2,3) where status = 1;
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids)
                .eq(Setmeal::getStatus,1);
        if (this.count(setmealLambdaQueryWrapper) > 0){
            //不能删除,抛出异常
            throw new CustomException("套餐正在售卖中,不能删除");
        }


        //可以删除
        //删除套餐表
        this.removeByIds(ids);
        //删除对应菜品表
        //delete * from setmeal_dish where setmeal_id in(1,2,3);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper  =new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据--setmealDish
        setmealDishService.removeByIds(ids);

    }

    /**
     * 修改套餐表和套餐-菜品表
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        //更新dish表基本信息
        this.updateById(setmealDto);

        //修改菜品口味表
        //先删除原本的菜口味表,
        LambdaQueryWrapper<SetmealDish>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //添加当前提交过来的口味数据--dish_flavor表的insert操作
        List<SetmealDish> setmealDishesList = setmealDto.getSetmealDishes();
        setmealDishesList = setmealDishesList.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishesList);

    }

}
 